#!/usr/bin/env perl

$DEF_DIR="..";
$version="0.33";
$cxx="-c++";
$shadow="-shadow";
$language="perl5";
$pkg="scriptDEF";
$module="DEF";

use ExtUtils::MakeMaker;

while($ARGV[0] =~ /^-/o){
   $arg=shift @ARGV;
   if($arg eq "-v"){
      $version=shift @ARGV;
   }elsif($arg =~ /^-py/o){
      $language="python";
   }elsif($arg =~ /^-perl/o){
      $language="perl5";
   }elsif($arg =~ /^-tcl/o){
      $language="tcl8";
      $shadow="";
   }elsif($arg eq "-c++"){
      $cxx="-c++";
   }elsif($arg eq "-shadow"){
      $shadow="-shadow";
   }elsif($arg eq "-pkg"){
      $pkg=shift @ARGV;
   }
}

if(scalar @ARGV>0){ $module=shift @ARGV;}
if(!defined $module){
   print STDERR "Please specify a module name!\n";
   exit 1;
}
if(-e "callback.i"){ 
   # Make sure system recompiles
   unlink "callback.i";
}
if(-e "class.i"){
   unlink "class.i";
}
if(-e "DEF.so"){ unlink "DEF.so";}

$CC="gcc";
$csuffix="";
if($cxx eq "-c++"){
   $CC="g++";
   $csuffix="c";
}

if($language eq "perl5" && !-e "test_$module.pl"){
   open(F,">test_$module.pl");
   print F << "END";
#!/usr/bin/env perl -Iblib/arch
use $module;\npackage $module;
print "$module loaded OK!\\n";
END
   close F;
}elsif($language eq "python" && !-e "test_$module.py"){
   open(F,">test_$module.py");
   print F << "END";
#!/usr/bin/env python
import $module
print "$module loaded OK!\\n"
END
   close F;
}elsif($language eq "tcl8" && !-e "test_$module.tcl"){
   open(F,">test_$module.tcl");
   print F << "END";
load ./$module.so $module
puts "$module loaded OK!"
END
   close F;
}


if(exists $ENV{'DEF_DIR'}){
   $DEF_DIR=$ENV{'DEF_DIR'};
}

if(!-r "$DEF_DIR/defrReader.hpp"){
   $DEF_DIR.="/def";
   if(!-r "$DEF_DIR/defrReader.hpp"){
      die "Please setenv DEF_DIR to point to the cadence DEF parser path!\n";
   }
}

WriteMakefile(
  'NAME' => $module,
  'INC' => "-I$DEF_DIR -I/usr/local/include/python2.0 -I/mnt/asic14a/phchenb/include/python2.0 -I/mnt/asic14a/phchenb/include -I/usr/local/include",
#  'LIBS' => ['-L/usr/local/lib/python2.0 -L/mnt/asic14a/phchenb/lib -lm -L/usr/local/lib  -L/mnt/asic14a/phchenb/lib/python2.0'],
  'LIBS' => ['-L/usr/local/lib/python2.0 -L/mnt/asic14a/phchenb/lib -lm -L/mnt/asic14a/phchenb/lib/python2.0'],
  'OBJECT'=> "${module}_wrap.o $DEF_DIR/libdef.a",
  'LD' => 'g++',
  'LDDLFLAGS' => "-shared", 
#  'CCFLAGS' => '-g -Dbool=char',
  'CCFLAGS' => '-g -fPIC -traditional',
  'OPTIMIZE' => ' ',
  'VERSION' => $version,
  'CC' => $CC
);


sub MY::postamble {

if($language eq "python"){
   $remove_aliases=<<"END";
	perl -ple 's/def\\s+print\\s*\\(/def Print(/o;' DEF.py > tmp.py
	perl -ple 's/def\\s+in\\s*\\(/def In(/o; s/def\\s+from\\s*\\(/def From(/o;' tmp.py > DEF.py
	rm tmp.py
END
}else{
   $remove_aliases="";
}
$attach_ptr2obj="";
if($language eq "tcl8"){
   $attach_ptr2obj="cat tclptr2obj.c >> DEF_wrap.c";
}

return << "END";
DEF_DIR = $DEF_DIR

python:	${module}_wrap.o
	\$(LD) \$(LDDLFLAGS) \$(OBJECT) \$(EXTRALIBS) -o ${module}${csuffix}.so

tcl:	${module}_wrap.o
	\$(LD) \$(LDDLFLAGS) \$(OBJECT) \$(EXTRALIBS) -o ${module}.so

DEF_wrap.c ::	DEF.i genCbkFn.pl class.i defwWriter.i callback.i defiEnum.i
	/data/projects/def_parser/working/swig/bin/swig -$language $shadow -c++ DEF.i
	$attach_ptr2obj
$remove_aliases
class.i :: xtrclass.pl 	
	perl xtrclass.pl -$language $DEF_DIR/*.hpp > class.i
defiEnum.i ::	$DEF_DIR/defrReader.hpp $DEF_DIR/defwWriterCalls.hpp
	perl xtrenum.pl $DEF_DIR/defrReader.hpp $DEF_DIR/defwWriterCalls.hpp > defiEnum.i
defwWriter.i :: $DEF_DIR/defwWriter.hpp
	perl -nle 's/EXTERN//o; if(\$\$_ !~ /^#/){ print;}' $DEF_DIR/defwWriter.hpp > defwWriter.i
callback.i :: genCbkFn.pl mkcallback.pl class.i
	perl genCbkFn.pl -$language $DEF_DIR/*.hpp > callback.i
	rm cbk.*

DEF.i:	class.i defwWriter.i callback.i 

tar:	
	(\\rm -rf blib; \\rm *.bs *.so *.pm pm_to_blib tmp* *~ *.pyc callback.i DEF.py class.i\\
         lex.*.c *.tab.* core sdfood verood *_wrap.* *.so *.o *.doc; \\
         cd ..;tar cvf $pkg-$version.tar $pkg-$version; \\
         gzip $pkg-$version.tar)
END
}

